const app = require('../../live.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    halfPath: '',
    loadingB: true
  },

  // 监听输入标题
  titleInput: function (e) {
    this.setData({
      title: e.detail.value
    })
  },

  // 监听输入积分数
  integralInput: function (e) {
    this.setData({
      integral: e.detail.value
    })
  },

  // 监听输入时间
  timeInput: function (e) {
    this.setData({
      time: e.detail.value
    })
  },

  // 跳转到推流
  toPush: function (e) {
    var that = this
    let url = encodeURIComponent(e.currentTarget.dataset.url)
    let title = that.data.title;
    let integral = that.data.integral;
    console.log(integral)
    var time = that.data.time;
    if (integral == '' || integral == null){
      integral = ''
    }
    if (time == '' || time == null) {
      time = ''
    }
   
    if (title == '' || title == null) {
      wx.showToast({
        title: '请输入标题！',
        icon: 'none',
        duration: 1500
      })
      return
    }
    // console.log(that.data.account + that.data.searchName + that.data.halfPath)
    if (that.data.halfPath === '') {
      wx.showToast({
        title: '请上传封面！',
        icon: 'none',
        duration: 1500
      })
      that.setData({
        //将临时变量赋值给已经在data中定义好的变量
        halfPath: that.data.halfPath
      })
    } else {
      console.log(that.data.netId + '///' + title + '///' + that.data.halfPath + '///' + time + '///' + integral)
    wx.request({
      url: app.pageConfig.jnbyUrl+'/sunday/small/live/saveLive',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        netId: that.data.netId,
        name: title,
        image: that.data.halfPath,
        scoreTime: time,
        integeral: integral
      },
      success: function (res) {
        console.log(res)
        switch (res.data.code) {
          case 0:
            let url = encodeURIComponent(res.data.result.pushUrl)
            // console.log(res.data.result.pushUrl)
            wx.redirectTo({
              url: `../push/push?url=${url}`
            })
            break;
          case 4:
            wx.showToast({
              icon: 'none',
              title: '创建频道失败',
            })
            break;
          case 5:
            wx.showToast({
              icon: 'none',
              title: '创建聊天室失败',
            })
            break;
          case 6:
            wx.showToast({
              icon: 'none',
              title: '频道名称已存在',
            })
            break;
          case 7:
            wx.showToast({
              icon: 'none',
              title: '自动录制失败',
            })
            break;
        }
        
      },
      fail: function (err) {
        wx.showToast({
          title: '请求失败！',
        })
        console.log(err)
      },
      complete: function () {
        // wx.hideLoading()
      }
    })
    }
  },

  // 上传封面
  chooseImage: function () {
    var that = this;
    that.setData({
      loadingB: false
    })
    wx.chooseImage({
      // 设置最多可以选择的图片张数，默认9,如果我们设置了多张,那么接收时//就不在是单个变量了,
      count: 1,
      sizeType: ['original'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (response) {
        // 获取成功,将获取到的地址赋值给临时变量
        var tempFilePaths = response.tempFilePaths;
        // console.log(tempFilePaths)
        that.setData({
          //将临时变量赋值给已经在data中定义好的变量
          coverUrl: tempFilePaths
        })
        wx.uploadFile({
          url: app.pageConfig.jnbyUrl +'/sunday/front/upload/uploadSingle',
          filePath: tempFilePaths[0],
          name: 'file',
          method: "POST",
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            // console.log('img'+res)
            //var data = res
            that.setData({
              //将临时变量赋值给已经在data中定义好的变量
              fileData: res.data
            })

            let data = JSON.parse(res.data)
            //console.log("code" + data.code)
            console.log("半路经" + data.result.halfPath)
            that.setData({
              //将临时变量赋值给已经在data中定义好的变量
              halfPath: data.result.halfPath,
              loadingB: true
            })
          }
        })
      },
      fail: function () {
        wx.showToast({
          title: '图片上传失败，请重试！',
          icon: 'none',
          duration: 1500
        })
        that.setData({
          loadingB: true
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      netId: options.netId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})